package com.chuyangc.controller;


import com.chuyangc.entity.Goods;
import com.chuyangc.service.GoodsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 欧楚杨
 * @since 2022-05-04
 */
@RestController
@RequestMapping("/chuyangc/goods")
@Api(tags = "商品相关的请求")
public class GoodsController {
    @Autowired
    GoodsService goodsService;
    /**
     * 查询所有商品
     * @return List
     */
    @GetMapping("/list")
    @ApiOperation("查询所有商品")
    public List<Goods> listGoods(){
        return goodsService.list();
    }

    /**
     * 商品分页
     * @return List
     */
    @GetMapping("/pagelist/{pageNum}")
    @ApiOperation("分页")
    public PageInfo<Goods> pageListGoods(@PathVariable("pageNum")int pageNum){
        int pageSize=3;
        PageHelper.startPage(pageNum, pageSize);
        List<Goods> goodsList = goodsService.list();
        PageInfo<Goods> goodsPageInfo = new PageInfo<>(goodsList);
        return goodsPageInfo;
    }

    /**
     * 商品记录数
     */
    @GetMapping("/pagelist/count")
    @ApiOperation("记录数")
    public int pageListCount(){
        return goodsService.count();
    }

    /**
     * 根据商品id删除商品
     * @return boolean
     */
    @DeleteMapping("/delete/{gid}")
    @ApiOperation("根据商品id删除商品")
    public boolean deleteGoods(@PathVariable("gid")Integer gid){
        return this.goodsService.removeById(gid);
    }

    /**
     * 根据商品id查询内容
     * @param gid
     * @return
     */
    @GetMapping("/find/{gid}")
    @ApiOperation("根据商品id查询内容")
    public Goods findGoods(@PathVariable("gid")Integer gid){
        return this.goodsService.getById(gid);
    }

    /**
     *根据传过来的goods实体更新内容
     * @param goods
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("根据传过来的goods实体更新内容")
    public boolean updateGoods(@RequestBody Goods goods){
        return this.goodsService.updateByGid(goods);
    }

    /**
     * 根据传过来的goods实体添加库记录
     * @param goods
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("根据传过来的goods实体添加库记录")
    public boolean addGoods(@RequestBody Goods goods){
        return this.goodsService.save(goods);
    }
}
